<template>
  <a-table :columns="columns" :data-source="dataSource" bordered>
    <template #bodyCell="{ column, text, record }">
      <template v-if="['name', 'age', 'address'].includes(column.dataIndex)">
        <div>
          <a-input
              v-if="editableData[record.key]"
              v-model:value="editableData[record.key][column.dataIndex]"
              style="margin: -5px 0"
          />
          <template v-else>
            {{ text }}
          </template>
        </div>
      </template>
      <template v-else-if="column.dataIndex === 'operation'">
        <div class="editable-row-operations">
          <span v-if="editableData[record.key]">
            <a-typography-link @click="save(record.key)">Save</a-typography-link>
            <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)">
              <a>Cancel</a>
            </a-popconfirm>
          </span>
          <span v-else>
            <a @click="edit(record.key)">Edit</a>
          </span>
        </div>
      </template>
    </template>
  </a-table>
</template>
<script setup lang="ts">
import { cloneDeep } from 'lodash-es';
import {defineComponent, onMounted, reactive, ref} from 'vue';
import type { UnwrapRef } from 'vue';
import myAxios from "../../plugins/myAxios";
import {message} from "ant-design-vue";
import type { Dayjs } from 'dayjs';
import dayjs from "dayjs";
const columns = [

  {
    title: '学号',
    dataIndex: 'studentNo',
    width: '15%',
  },
  {
    title: '姓名',
    dataIndex: 'studentName',
    width: '10%',
  },
  {
    title: '姓别',
    dataIndex: 'studentGender',
    width: '10%',
  },
  {
    title: '家庭住址',
    dataIndex: 'studentAddress',
    width: '10%',
  },
  {
    title: '出生年月',
    dataIndex: 'studentBirthdate',
    width: '10%',
  },
  {
    title: '专业',
    dataIndex: 'studentMajor',
    width: '10%',
  },
  {
    title: '班级号',
    dataIndex: 'studentClassNo',
    width: '10%',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: '10%',
  },

];
interface DataItem {
  key: string;
  name: string;
  age: number;
  address: string;
}
const data: DataItem[] = [];
for (let i = 0; i < 100; i++) {
  data.push({
    key: i.toString(),
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`,
  });
}

const studentDate = ref()

    const dataSource = ref(studentDate);
    const editableData: UnwrapRef<Record<string, DataItem>> = reactive({});

    const edit = (key: string) => {
      editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
    };
    const save = (key: string) => {
      Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
      delete editableData[key];
    };
    const cancel = (key: string) => {
      delete editableData[key];
    };


onMounted(()=>{
  getStudentList()
})
    const getStudentList = async () =>{
      const res = await myAxios.get("/students/getSameClassStudentsInfo")
      if (res.data){
        studentDate.value = res.data
        studentDate.value = studentDate.value.map(item=>{
          item.createTime = dayjs(item.createTime).format('YYYY-MM-DD')
          return item
        })
        // console.log(studentDate.value)
      }else {
        message.error(res.message)
      }
    }

</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
